package handler

import (
	"2302a/tiktok_business/api/request"
	"2302a/tiktok_business/api/resp"
	"2302a/tiktok_business/global"
	"2302a/tiktok_business/internal/dao"
	"2302a/tiktok_business/internal/model"
	"2302a/tiktok_business/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"regexp"
)

func AddBarrages(c *gin.Context) {
	var req request.AddBarrages
	err := c.ShouldBind(&req)
	if err != nil {
		resp.Error(c, http.StatusUnauthorized, "参数信息错误", err.Error())
		return
	}
	if !pkg.SensitiveClient(req.Content) {
		resp.Error(c, http.StatusUnauthorized, "***", false)
		return
	}

	regexp.MustCompile("").Find([]byte(req.Content))
	if len(req.Content) >= 50 {
		resp.Error(c, http.StatusUnauthorized, "弹幕内容（长度 1-50 字）", false)
		return
	}

	var user model.Users
	userId := c.GetUint("user_id")
	global.DB.Find(userId, &user)
	fmt.Println(user.UserId)
	var video model.Videos
	global.DB.Find(&video)
	if video.VideoId == 0 {
		resp.Error(c, http.StatusUnauthorized, "视频不存在", false)
		return
	}
	comments := model.Barrages{
		VideoId:  video.VideoId,
		UserId:   userId,
		Content:  req.Content,
		Progress: req.Progress,
		Filterad: req.Filterad,
	}
	dao.Create(&comments)

	resp.Success(c, "弹幕实时发送成功", comments)
}
